package com.vcourse.service.admin.bo;

import com.vcourse.service.admin.entity.Permission;
import com.vcourse.service.admin.entity.Role;
import com.vcourse.service.admin.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zengsh123
 * @date 2020-06-17 15:18
 * SpringSecurity需要的用户详情
 */
public class AdminUserDetails implements UserDetails {
    /**
     * 后台用户信息
     */
    private User user;
    /**
     * 该用户拥有的角色列表
     */
    private List<String> permissionValueList;

    public AdminUserDetails(User user,List<String> permissionValueList) {
        this.user = user;
        this.permissionValueList = permissionValueList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return permissionValueList.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return !user.getDeleted();
    }
}
